import routeList from "@/utils/routeList";
export default {
  namespace: "common",
  state: {
    detailStatus: false,
  },
  subscriptions: {
    setup({ dispatch, history }: { dispatch: any; history: any }) {
      dispatch({ type: "queryUserInfo", payload: { history } });
    },
  },
  reducers: {
    queryUserInfo(state, { payload }) {
      const {
        history,
        history: {
          location: { pathname },
        },
      } = payload;

      //路由拦截操作
      if (pathname !== "/users/login" && pathname !== "/users/forgetPassword") {
        if (
          !sessionStorage.getItem("Token") ||
          !sessionStorage.getItem("userInfo") ||
          !sessionStorage.getItem("routeList")
        ) {
          history.replace("/users/login");
        } else {
          //满足条件
          sessionStorage.setItem("routeList", JSON.stringify(routeList));
        }
      } else {
        //当前在登录或者忘记密码页面不需要拦截
        sessionStorage.clear();
      }

      return state;
    },
    toggleDetail(state, { payload }) {
      return {
        ...state,
        detailStatus: payload,
      };
    },
  },
  effects: {},
};
